// 文件路径: src/main/java/com/chixing/controller/CouponController.java

package com.chixing.controller;

import com.chixing.entity.Coupon;
import com.chixing.service.ICouponService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 优惠券前端控制器
 * </p>
 *
 * @author liwenjing
 * @since 2025-10-07
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private ICouponService couponService;

    /**
     * 获取所有优惠券
     * @return 优惠券列表
     */
    @GetMapping("")
    public Result getAllCoupons() {
        try {
            List<Coupon> coupons = couponService.list();
            return Result.getSuccess(coupons);
        } catch (Exception e) {
            return Result.error("获取优惠券列表失败: " + e.getMessage());
        }
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable("id") Integer id){
        return couponService.getById(id);
    }
    @GetMapping("userId/{userId}/status/{couponStatus}")
    public Result getByStatus(@PathVariable("userId") Integer userId ,
                              @PathVariable("couponStatus") Integer couponStatus){
        return couponService.getByStatus(userId,couponStatus);
    }

    /**
     * 添加优惠券
     * @param coupon 优惠券对象
     * @return 添加结果
     */
    @PostMapping("")
    public Result addCoupon(@RequestBody Coupon coupon) {
        try {
            coupon.setCreateTime(LocalDateTime.now());
            coupon.setUpdateTime(LocalDateTime.now());
            boolean result = couponService.save(coupon);
            if (result) {
                return Result.saveSuccess("添加优惠券成功");
            } else {
                return Result.error("添加优惠券失败");
            }
        } catch (Exception e) {
            return Result.error("添加优惠券异常: " + e.getMessage());
        }
    }

    /**
     * 更新优惠券
     * @param couponId 优惠券ID
     * @param coupon 优惠券对象
     * @return 更新结果
     */
    @PutMapping("/{couponId}")
    public Result updateCoupon(@PathVariable Integer couponId, @RequestBody Coupon coupon) {
        try {
            coupon.setCouponId(couponId);
            coupon.setUpdateTime(LocalDateTime.now());
            boolean result = couponService.updateById(coupon);
            if (result) {
                return Result.updateSuccess("更新优惠券成功");
            } else {
                return Result.error("更新优惠券失败");
            }
        } catch (Exception e) {
            return Result.error("更新优惠券异常: " + e.getMessage());
        }
    }

    /**
     * 删除优惠券
     * @param couponId 优惠券ID
     * @return 删除结果
     */
    @DeleteMapping("/{couponId}")
    public Result deleteCoupon(@PathVariable Integer couponId) {
        try {
            boolean result = couponService.removeById(couponId);
            if (result) {
                return Result.deleteSuccess("删除优惠券成功");
            } else {
                return Result.error("删除优惠券失败");
            }
        } catch (Exception e) {
            return Result.error("删除优惠券异常: " + e.getMessage());
        }
    }
    @PostMapping("/receive/{userId}")
    public Result receiveCoupon(@PathVariable Integer userId, @RequestBody Coupon coupon) {
        try {
            boolean result = couponService.receiveCoupon(userId, coupon);
            if (result) {
                return Result.saveSuccess("领取优惠券成功");
            } else {
                return Result.error("领取优惠券失败，优惠券无效或已过期");
            }
        } catch (Exception e) {
            return Result.error("领取优惠券异常: " + e.getMessage());
        }
    }
}

